Page({
  data: {
    dateLayerVisible: false,
    houseLayerVisible: false,
    houseInfo: '',
    name: '',
    mobile: '',
    gender: 1,
    visitDate: '',
    houseList: [],
  },
  onLoad() {
    this.getHouseList()
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  //校验房屋信息
  checkHouse() {
    const vaild = Boolean(this.data.houseInfo)
    if (!vaild) return wx.utils.toast('请选择报修房屋!')
    return vaild
  },
  //校验姓名
  checkName() {
    const vaild = Boolean(this.data.name)
    if (!vaild) return wx.utils.toast('请输入姓名!')
    return vaild
  },
  //校验手机号码
  checkmobile() {
    const reg = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/
    const vaild = reg.test(this.data.mobile.trim())
    if (!vaild) return wx.utils.toast('请输入合法的手机号码!')
    return vaild
  },
  //校验预约时间
  checkDate() {
    const vaild = Boolean(this.data.visitDate)
    if (!vaild) return wx.utils.toast('请选择预约时间!')
    return vaild
  },
  //选择房屋
  selectHouse(e) {
    console.log(e);
    const { name, id: houseId } = e.detail
    this.setData({
      houseInfo: name, houseId
    })
  },
  //选择日期
  selectDate(e) {
    console.log(e.detail);
    this.setData({
      visitDate: wx.utils.formDate(e.detail),
      dateLayerVisible: false
    })
  },
  //获取审核通过的房屋
  async getHouseList() {
    const { data } = await wx.http.get('/house')
    // console.log(data, 5555);
    this.setData({
      houseList: data
    })
  },
  async goPassport() {
    if (!this.checkHouse()) return
    if (!this.checkName()) return
    if (!this.checkmobile()) return
    if (!this.checkDate()) return
    const { houseId, name, gender, mobile, visitDate } = this.data
    const res = await wx.http.post('/visitor', { houseId, name, gender, mobile, visitDate })
    console.log(res);
    wx.utils.toast('预约成功')
    wx.navigateTo({
      url: '/visitor_pkg/pages/passport/index?id=' + res.data.id,
    })
  },
})
